<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Member Controller.
 * 
 * @class     Member
 * @author    Iqbal
 * @version   1.0.0
 * @copyright Copyright (c) 2014 IPPI (http://www.ippi.or.id)
 */
class member extends CI_Controller {
    /**
	 * Constructor.
	 */
    function __construct() {
        parent::__construct();
        
        // Load model
        $this->load->model('model_member', 'm_member', TRUE);
    }
    
    /**
	 * Register member function.
     * @return AJAX String
	 */
    function register(){
        // Set variables
        $name   = $this->input->post("name");
        $email  = $this->input->post("email");
        $pass   = md5($this->input->post("password"));
        
        // Set Form validation rules
        $this->form_validation->set_rules('name', 'name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        
        // Check form validation rules
        if($this->form_validation->run() == FALSE){
            echo "2";
        }else if($this->m_member->count_by_email($email) > 0){
            echo "0";
        }else{
            $curdate        = date("Y-m-d h:i:s");
            $member_data    = array(
                "name"          =>  $name,
                "email"         =>  $email,
                "password"      =>  $pass,
                "datecreated"   =>  $curdate,
                "datemodified"  =>  $curdate
            );            
            $save = $this->m_member->save_data($member_data);
            
            if( $save ){ $this->login(); }
        }
    }
    
    /**
	 * Register member function.
     * @return AJAX String
	 */
    function resetpassword(){
        // Set variables
        $current_pass       = ippi_isset($this->input->post("current_pass"), '');
        $new_pass           = ippi_isset($this->input->post("new_pass"), '');
        $new_pass_confirm   = ippi_isset($this->input->post("new_passconfirm"), '');
        
        // Set Form validation rules
        $this->form_validation->set_rules('current_pass', 'Current Password', 'trim|required|xss_clean');
        $this->form_validation->set_rules('new_pass', 'New Password', 'trim|required|xss_clean');
        $this->form_validation->set_rules('new_passconfirm', 'Confirm New Password', 'trim|required|xss_clean');
        
        // Check form validation rules
        if($this->form_validation->run() == FALSE){
            echo "2"; die();
        }elseif($new_pass != $new_pass_confirm){
            echo "3"; die();
        }else{
            $member         = ippi_get_current_member();
            $check          = $this->model_member->count_by_password($member->email, md5($current_pass));
            
            if( $check > 0 ){
                $curdate        = date("Y-m-d H:i:s");
                $member_data    = array(
                    "password"      =>  md5($new_pass),
                    "datemodified"  =>  $curdate
                );            
                $save = $this->m_member->update_data($member->id_member, $member_data);
                
                if( $save ){ echo base_url('member/logout'); die(); }
            }else{
                echo "0"; die();
            }
        }
    }
    
    /**
	 * Login member function.
     * @return AJAX String
	 */
    function login() {
        // Set credential variable param
        $email      = ippi_isset($this->input->post("email"), 0);
        $pass       = md5( ippi_isset($this->input->post("password"), 0) );
        
        // Set Credential for login
        $credentials['email']       = $email;
        $credentials['password']    = $pass;
        
        // Sign On member
        $member     = $this->m_member->signon($credentials);
        
        // Response of signon member
        if ( $member == 'not_activated' ){
            echo 'Banned';
        } elseif ( $member == 'deleted' ){
            echo 'Deleted';
        } elseif ( $member == 'logged' ){
            echo 'Logged';
        } elseif ( $member ) {
            $member         = $this->ippi_member->member($member->id_member);
            $last_activity  = date('Y-m-d H:i:s', time() );
            
            $login_update   = array( 'login' => 1, 'last_activity' => $last_activity );
            $this->m_member->update_data($member->id_member, $login_update);
            
            // Set session data
            $session_data   = array(
                'id_member'     => $member->id_member,
                'email'         => $member->email,
                'name'          => $member->name,
                'last_activity' => $member->last_activity
            );
            // Set session
            $this->session->set_userdata('member_logged_in', $session_data);
            
            // Set cookie domain
            $cookie_domain  = str_replace(array('http://', 'https://', 'www.'), '', base_url());
            $cookie_domain  = '.' . str_replace('/', '', $cookie_domain);
            $expire         = 0;
            // Set cookie data
            $cookie         = array(
                'name'      => 'logged_in_'.md5('nonssl'),
                'value'     => $member->id_member,
                'expire'    => $expire,
                'domain'    => $cookie_domain,
                'path'      => '/',
                'secure'    => false,
            );
            // set cookie
            setcookie($cookie['name'], $cookie['value'],$cookie['expire'],$cookie['path'],$cookie['domain'],$cookie['secure']);
            
            echo base_url();
        } else {
            echo "Failed";
        }
    }
    
    /**
	 * Logout member function.
     * @return URL redirect page
	 */
    function logout(){
        // Load AJAX Chat
        require(AJAX_CHAT_PATH . 'lib/classes.php');
        
        // Set variables
        $member         = ippi_isset($this->session->userdata('member_logged_in'), '');
        $id_member      = ippi_isset($member['id_member'], 0);
        
        $this->m_member->update_data($id_member, array('login' => 0));
        auto_logout();
        
        redirect( base_url(), 'refresh' );
    }
    
    /**
	 * Auto Logout member function.
     * @return AJAX String
	 */
    function autologout(){
        // Set variables
        $response   = 'Logged';
        $member     = ippi_get_current_member();
        $id_member  = ippi_isset($member->id_member, 0);
        
        // Check response of check member id
        if( $id_member > 0 ){
            $loggen_in          = check_member_login($id_member, 'auto');
        
            if( $loggen_in == 1 )       { $response = 'Logged'; }
            elseif( $loggen_in == 2 )   { $response = base_url(); }
            elseif( $loggen_in == 0 )   { $response = 'Logged'; }
        } 
        
        // Return response
        echo $response;
    }
}

/* End of file member.php */
/* Location: ./application/controllers/member.php */